require('events').EventEmitter.defaultMaxListeners = 50
const path = require('path')
const env = process.env
const isProd = env.MODE === 'prod'
let envFileName = ''
if (['DEV', 'TEST', 'UAT', 'PRD'].indexOf(env.MODE) > -1) {
  require('dotenv').config({
    path: `.env.${env.MODE}`
  })
  envFileName = `.env.${env.MODE}`
} else {
  require('dotenv').config()
  envFileName = '.env'
}

const apiServer = process.env.API_SERVER
const guokeMdoorServeDev = process.env.guokeMdoorServeDev
if (!isProd) {
  console.log(
    `当前本地环境变量值: \n apiServer: ${apiServer} \n 代理地址: ${guokeMdoorServeDev}`
  )
} else {
  console.log(`当前线上环境变量值: \n 公共地址 : ${process.env.PUBLIC_PATH}`)
}

const config = {
  env: {
    '/guoke-deepexi-product-center/api/v1': guokeMdoorServeDev,
    '/m-staff-center/api/v1': guokeMdoorServeDev,
    '/guoke-channel-aggregation-center/': guokeMdoorServeDev,
    '/guoke-deepexi-auth-center/api/v1/': guokeMdoorServeDev,
    '/guoke-deepexi-interface-center': guokeMdoorServeDev,
    '/guoke-deepexi-message-center/api/v1/': guokeMdoorServeDev,
    '/guoke-deepexi-file-library/': guokeMdoorServeDev,
    '/m-data-center/': guokeMdoorServeDev,
    '/guoke-deepexi-marking-center/api/v1': guokeMdoorServeDev
  },
}

let axios = {
  proxy: true,
}

// 如果生产指定apiServer, 则使用绝对路径请求api
if (isProd && apiServer) {
  axios = {
    proxy: false,
    baseURL: apiServer,
  }
}

module.exports = {
  // dir在前，srcDir在后
  dir: {
    assets: '../m-front-common/pc/assets',
    layouts: 'layouts',
    middleware: 'middleware',
    pages: 'pages',
    static: '../m-front-common/pc/static',
    store: 'store',
  },
  srcDir: 'src/',
  mode: 'spa',
  env: {
    COOKIE_PATH: process.env.COOKIE_PATH || '/',
  },
  proxy: config.env,
  router: {
    base: process.env.ROUTER_BASE,
    middleware: ['meta', 'auth'],
    mode: 'hash',
  },
  server: {
    port: 3001,
    host: '0.0.0.0',
  },
  /*
   ** Build configuration
   */
  build: {
    publicPath: process.env.PUBLIC_PATH,
    extractCSS: isProd,
    styleResources: {
      less: '../m-front-common/pc/assets/m-var.less',
    },
    // babel: {
    //   plugins: [
    //     [
    //       'component',
    //       {
    //         libraryName: 'element-ui',
    //         styleLibraryName: 'theme-chalk',
    //       },
    //     ],
    //   ],
    // },
    /*
     ** Run ESLint on save
     */
    extend(config, {
      isDev
    }) {
      // if (isDev && process.client) {
      //   config.module.rules.push({
      //     enforce: 'pre',
      //     test: /\.(js|vue)$/,
      //     loader: 'eslint-loader',
      //     exclude: /(node_modules)/,
      //   })
      // }
      const alias = (config.resolve.alias = config.resolve.alias || {})
      alias['@components'] = path.join(
        __dirname,
        'm-front-common/pc/components'
      )
      alias['@api'] = path.join(__dirname, 'm-front-common/pc/api')
      alias['@services'] = path.join(__dirname, 'm-front-common/pc/services')
      alias['@const'] = path.join(__dirname, 'm-front-common/pc/const')
      alias['@mixins'] = path.join(__dirname, 'm-front-common/pc/mixins')
      alias['@assets'] = path.join(__dirname, 'm-front-common/pc/assets')
      alias['@pc'] = path.join(__dirname, 'm-front-common/pc')
    },
  },
  /*
   ** Headers of the page
   */
  head: {
    title: '',
    meta: [{
      charset: 'utf-8'
    },
    {
      name: 'viewport',
      content: 'width=device-width, initial-scale=1'
    },
    {
      'http-equiv': 'x-ua-compatible',
      content: 'IE=edge, chrome=1'
    },
    {
      hid: 'description',
      name: 'description',
      content: '',
    },
    ],
    script: [{
      src: 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'
    },
    {
      src: 'https://m.met-ec.com/guoke-deepexi-utils-server-h5/icon/channel-icon/iconfont.js'
    }
      // {src: '../m-front-common/pc/static/mys.js'}
    ],
    link: [
      {
        rel: 'icon',
        type: 'image/x-icon',
        href: 'https://acn2ustorageaccountm.blob.core.chinacloudapi.cn/guoke-channel-aggregation-center/5hmoqxr9z033lmi2qartn2wifa8274pn.png'
      },
      // 国科渠道
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: '//at.alicdn.com/t/font_1242246_wa1wt85uy5.css'
      },
      // 国科商城
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: '//at.alicdn.com/t/font_1403616_ewms796lqef.css'
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/guoke-deepexi-utils-server-h5/icon/sub-sys-icon/iconfont.css',
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/guoke-deepexi-utils-server-h5/icon/agency-sass-icon/iconfont.css',
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://md.heng-tai.com.cn/guoke-deepexi-utils-server-h5/icon/m-pc-menu-icon/iconfont.css',
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/m-static-resource/m-icon/m-pc-public/iconfont.css',
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/m-static-resource/m-icon/m-pc-menu/iconfont.css',
      },
    ],
  },
  /*
   ** Customize the progress bar color
   */
  loading: {
    color: '#5D81F9',
  },
  /**
   * Share variables, mixins, functions across all style files (no @import needed)
   * @Link https://github.com/nuxt-community/style-resources-module/
   */
  css: [{
    src: '../m-front-common/pc/assets/m-reset.less',
    lang: 'less',
  },],
  plugins: [{
    src: '../m-front-common/pc/plugins/axios'
  },
  {
    src: '../m-front-common/pc/plugins/m-element'
  },
  {
    src: '../m-front-common/pc/plugins/confirmCustom/index'
  },
  {
    src: '../m-front-common/pc/plugins/const'
  },
  {
    src: '../m-front-common/pc/plugins/router'
  },
  {
    src: '../m-front-common/pc/plugins/icon-font'
  },
  {
    src: '../m-front-common/pc/plugins/pdfViewer/index'
  },
  {
    src: '../m-front-common/pc/plugins/viewer'
  },
  {
    src: '../m-front-common/pc/plugins/cropper',
    ssr: false
  },
  {
    src: '../m-front-common/pc/plugins/fullpage'
  },
  { src: '../m-front-common/pc/plugins/m-plugins' },
  ],
  modules: [
    // Doc: https://github.com/nuxt-community/style-resources-module
    '@nuxtjs/style-resources',
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    // Doc: https://github.com/nuxt-community/dotenv-module
    ['@nuxtjs/dotenv', {
      path: './',
      filename: envFileName
    }],
  ],
  axios,
}